package net.sf.eclipseshell.clojureenvironment.clojureinterfacespec;

import java.io.PushbackReader;
import java.io.StringReader;

import junit.framework.TestCase;
import clojure.lang.Compiler;
import clojure.lang.LispReader;
import clojure.lang.RT;
import clojure.lang.Symbol;
import clojure.lang.Var;

public class ClojureBaseTestCase extends TestCase {
	
	

	static final Symbol USER = Symbol.create("user");
	static final Symbol CLOJURE = Symbol.create("clojure.core");

	static final Var in_ns = RT.var("clojure.core", "in-ns");
	static final Var refer = RT.var("clojure.core", "refer");
	static final Var ns = RT.var("clojure.core", "*ns*");
	static final Var compile_path = RT.var("clojure.core", "*compile-path*");
	static final Var warn_on_reflection = RT.var("clojure.core", "*warn-on-reflection*");
	static final Var print_meta = RT.var("clojure.core", "*print-meta*");
	static final Var print_length = RT.var("clojure.core", "*print-length*");
	static final Var print_level = RT.var("clojure.core", "*print-level*");
	static final Var star1 = RT.var("clojure.core", "*1");
	static final Var star2 = RT.var("clojure.core", "*2");
	static final Var star3 = RT.var("clojure.core", "*3");
	static final Var stare = RT.var("clojure.core", "*e");

	
	protected Object evalCode(String code) throws Exception {
			Var.pushThreadBindings(
					RT.map(ns, ns.get(),
					       warn_on_reflection, warn_on_reflection.get(),
					       print_meta, print_meta.get(),
					       print_length, print_length.get(),
					       print_level, print_level.get(),
					       compile_path, "classes",
					       star1, null,
					       star2, null,
					       star3, null,
					       stare, null));


			
			try{
				in_ns.invoke(USER);
				refer.invoke(CLOJURE);
				
				Object EOF = new Object();
				Object r = LispReader.read(new PushbackReader(new StringReader(code)), false, EOF, false);
				if(r == EOF)
				{
					System.out.println();
				}
				Object ret = Compiler.load(new StringReader(code));
//				RT.print(ret, fConsoleOutput);

				return ret;
			} catch(Exception e){
				e.printStackTrace();
			}
			return null;
	}
}
